from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.files import copy,rename,rm
from os.path import join

class SimplemqCppConan(ConanFile):
    name = "simplemq-cpp"
    version = "master"
    settings = "os", "compiler", "build_type", "arch"
    description = "simple Message Queue, 实现支持消息推送的中间件"
    url = "https://gitee.com/l0km/simplemq-cpp"
    license = "BSD-2-Clause"
    author = "guyadong 10km0811@sohu.com"
    topics = ("messagequeue", "c++11","json")

    package_type = "header-library"
    settings = "os", "arch", "compiler", "build_type"
    options = {"build_test": [True, False]}
    default_options = {"build_test": False}

    requires = "common_source_cpp/1.0.1"

    # exports_sources = "include/*"

    def export(self):
        copy(self,"conanfile_master.py",self.recipe_folder, self.export_folder)
        rename(self,join(self.export_folder,"conanfile_master.py"),join(self.export_folder,"conanfile.py"))

    def source(self):
        # 从远端仓库下载指定分支代码
        # self.run("git clone -b " + self.version + " " + self.url + ".git")
        git = Git(self)
        clone_args = ['--branch', self.version]
        git.clone(url=self.url + ".git", args=clone_args, target=".")

    def requirements(self):
        if self.options.build_test  :
            self.requires("cpp_redis/master")
            self.requires("paho-mqtt-cpp/[>=1.2.0]")

    def package(self):
        # 确保当前仓库与远程仓库同步
        git = Git(self)
        self.output.info(git.run("pull"))
        copy(self, "*",join(self.source_folder,"include"),join(self.package_folder,"include"))
        copy(self,"LICENSE", self.source_folder,join(self.package_folder,"licenses"))

    def package_id(self):
        self.info.clear()

    def package_info(self):
        if self.settings.compiler == "msvc":
            self.cpp_info.defines = ["NOMINMAX"]
        if self.settings.os == "Android":
            self.cpp_info.libs = ["log"]
        if self.settings.os == "Linux":
            self.cpp_info.libs = ["pthread","dl"]

